import axios from 'axios'
import { ElLoading, ElMessageBox, ElNotification } from 'element-plus'
import sysConfig from '@/config'
// import tool from '@/utils/tool';
// import { getToken } from '@/utils/auth'
// 实例化取消请求对象
// const cancelRequest = new CancelRequest()

const isDev=import.meta.env.MODE==="development"
axios.defaults.baseURL = isDev?'/api/':''

axios.defaults.timeout = sysConfig.TIMEOUT
let loading
let timer
// HTTP request 拦截器
axios.interceptors.request.use(
	(config) => {
		if (config.loading === true) {
			timer = setTimeout(() => {
				loading = ElLoading.service({
					background: 'rgba(0,0,0,0)'
				})
			})
		}
		return config
	},
	(error) => {
		return Promise.reject(error)
	}
)

// HTTP response 拦截器
axios.interceptors.response.use(
	(response) => {
		if (timer) {
			clearTimeout(timer)
			loading && loading.close()
		}
		if(response.status===200&&response.data.code==200){
			return response.data
		}
	},
	(error) => {
		loading && loading.close()
		return Promise.reject(error.response)
	}
)

const http = {

	/** get 请求
	 * @param  {string} url
	 * @param  {{}} params
	 * @param  {参数} config
	 */
	get: function(url, params = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'get',
				url: url,
				params: params,
				...config
			}).then((response) => {
				if(config.isRes){
					resolve(response)
				}else{
					resolve(response.data)

				}
			}).catch((error) => {
				reject(error)
			})
		})
	},

	/** post 请求
	 * @param  {接口地址} url
	 * @param  {请求参数} data
	 * @param  {{loading: boolean}} config
	 */
	post: function(url, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'post',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response.data)
			}).catch((error) => {
				reject(error)
			})
		})
	},

	/** put 请求
	 * @param  {接口地址} url
	 * @param  {请求参数} data
	 * @param  {参数} config
	 */
	put: function(url, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'put',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response.data)
			}).catch((error) => {
				reject(error)
			})
		})
	},

	/** patch 请求
	 * @param  {接口地址} url
	 * @param  {请求参数} data
	 * @param  {参数} config
	 */
	patch: function(url, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'patch',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response.data)
			}).catch((error) => {
				reject(error)
			})
		})
	},

	/** delete 请求
	 * @param  {接口地址} url
	 * @param  {请求参数} data
	 * @param  {参数} config
	 */
	delete: function(url, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'delete',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response.data)
			}).catch((error) => {
				reject(error)
			})
		})
	},

	/** jsonp 请求
	 * @param  {接口地址} url
	 * @param  {JSONP回调函数名称} name
	 */
	jsonp: function(url, name = 'jsonp') {
		return new Promise((resolve) => {
			const script = document.createElement('script')
			const _id = `jsonp${Math.ceil(Math.random() * 1000000)}`
			script.id = _id
			script.type = 'text/javascript'
			script.src = url
			window[name] = (response) => {
				resolve(response.data)
				document.getElementsByTagName('head')[0].removeChild(script)
				try {
					delete window[name]
				} catch (e) {
					window[name] = undefined
				}
			}
			document.getElementsByTagName('head')[0].appendChild(script)
		})
	}
}

export default http
